package com.ruoyi.orderManagement.listener;

import com.rabbitmq.client.Channel;
import com.ruoyi.orderManagement.domain.Order;
import com.ruoyi.orderManagement.service.IOrderService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

/**
 * 监听超时未支付的订单
 */
@Component
@RabbitListener(queues = "order.dlx.queue2")
@Slf4j
public class OrderStatusListener {
    @Autowired
    private IOrderService orderService;
    @SneakyThrows
    @RabbitHandler
    public void handle(Order order, Message message, Channel channel,
                       @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag) {
        log.info("监听到超时未支付的订单，订单号：{}", order.getOrderId());
        // 处理订单超时未支付，修改订单状态为已超时
        orderService.updateOrderStatus(order.getOrderId(), "3");
        channel.basicAck(deliveryTag, false);
    }
}
